using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace aaaaa
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public static BasicEffect basicEffect;
        SpaceShip3D tras;
        SpaceShip3D frente;
        SpaceShip3D direita;
        SpaceShip3D esquerda;
        SpaceShip3D player;

        SkySphere skySphere;

        TPSCamera camera;
        //TPSCamera cameraRetrovisor;

        Viewport viewportPadrao;
        Viewport viewportRetrovisor;

        public static KeyboardState keyboardState;

        public static Game1 Instance;

        Light none;
        Light defaultIlumination;
        Light oneLight;
        Light emissiveOne;
        Light threeLight;
        Light fog;

        public Game1()
        {
            Instance = this;

            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            tras = new SpaceShip3D(Content.Load<Model>("Space Shooter/Space_Shooter"));
            frente = new SpaceShip3D(Content.Load<Model>("Space Shooter/Space_Shooter"));
            esquerda = new SpaceShip3D(Content.Load<Model>("Space Shooter/Space_Shooter"));
            direita = new SpaceShip3D(Content.Load<Model>("Space Shooter/Space_Shooter"));
            player = new SpaceShip3D(Content.Load<Model>("Space Shooter/Space_Shooter"));

            tras.Position = new Vector3(0, 0, 20);
            frente.Position = new Vector3(0, 0, -20);
            esquerda.Position = new Vector3(-20, 0, 0);
            direita.Position = new Vector3(20, 0, 0);

            player.Position = new Vector3(0, 0, -1);
            player.ifPlayer = true;

            player.Scale = 0.5f;

            skySphere = new SkySphere(Content, GraphicsDevice,
                "Black_sky");

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            camera = new TPSCamera();

            camera.Position = new Vector3(0, 0, 0);
            camera.Target = new Vector3(0, 0, -1);

            //cameraRetrovisor = new TPSCamera();
            //cameraRetrovisor.Position = camera.Position;
            //cameraRetrovisor.ViewMatrix = camera.ViewMatrix;
            //cameraRetrovisor.ProjectionMatrix = camera.ProjectionMatrix;
            //cameraRetrovisor.Target = new Vector3(0, 0, 1);

            viewportPadrao = GraphicsDevice.Viewport;
            viewportRetrovisor = new Viewport(0, 0, 160, 120);

            none = new Light(Light.Type.None);
            defaultIlumination = new Light(Light.Type.DefautIlumination);
            oneLight = new Light(Light.Type.OneDirection, Color.Yellow, new Vector3(-1, 0, 0));

            emissiveOne = new Light(Light.Type.Emissive, Color.Red);
                        
            fog = new Light(Light.Type.Fog, 20, 60, Color.Black);


            threeLight = new Light(Light.Type.ThreeDirections);
            threeLight.diffuseDirection0 = new Vector3(1, 0, 0);
            threeLight.diffuseDirection1 = new Vector3(0, 1, 0);
            threeLight.diffuseDirection2 = new Vector3(0, 0, 1);
            threeLight.diffuseColor0 = new Color(255, 0, 0);
            threeLight.diffuseColor1 = new Color(0, 255, 0);
            threeLight.diffuseColor2 = new Color(0, 0, 255);
            

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {

            keyboardState = Keyboard.GetState();
            foreach (GameObject3D gO3D in GameObject3D.list)
            {
                gO3D.Update(gameTime);
            }

            camera.Update(gameTime, player.RotationY, player.Position);

            //oneLight.diffuseDirection0.X -= 0.1f;
            //oneLight.diffuseDirection0.Y -= 0.1f;
            //oneLight.diffuseDirection0.Z -= 0.1f;

            //cameraRetrovisor.Update(gameTime, true);

            //if (Game1.keyboardState.IsKeyDown(Keys.Right))
            //{
            //    Position.X += 1;
            //}

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            skySphere.Draw(camera, GraphicsDevice);
            //GraphicsDevice.Viewport = viewportPadrao;

            foreach (GameObject3D gO3D in GameObject3D.list)
            {
                gO3D.Draw(camera, emissiveOne);
            }
            //foreach (GameObject3D gO3D in GameObject3D.list)
            //{
            //    //gO3D.Draw(cameraRetrovisor);
            //}

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

